<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="hadd(1,0)">添加权限</el-button>
      <el-table :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type===1" size="mini" type="text" @click="hadd(2,row.id)">添加</el-button>
            <el-button size="mini" type="text" @click="hEdit(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="hDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
      :title="showText"
      :visible="showDialog"
      @close="hCancel"
    >
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" style="width: 90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" style="width: 90%" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description" style="width: 90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="form.enVisible"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="6">
              <el-button size="mini" type="primary" @click="hSubmit">确定</el-button>
              <el-button size="mini" @click="hCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { addPermission, getPermissionList, updatePermission, getPermission, delPermission } from '@/api/permission'
import { arrToTree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      isEdit: false,
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: 1,
        pid: '',
        type: ''
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '必填', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showText() {
      return this.form.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await getPermissionList()
      console.log(res.data)
      this.list = arrToTree(res.data, 0)
    },
    hadd(type, id) {
      this.showDialog = true
      // console.log(type, id)
      this.form.pid = id
      this.form.type = type
      this.isEdit = false
    },
    hSubmit() {
      this.$refs.form.validate(async valid => {
        if (this.isEdit) {
          await updatePermission(this.form)
          this.$message.success('修改成功')
        } else {
          await addPermission(this.form)
          this.$message.success('添加成功')
        }
        this.showDialog = false
        this.getList()
      })
    },
    hCancel() {
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: 1,
        pid: '',
        type: ''
      }
      this.showDialog = false
    },
    async hEdit(id) {
      this.showDialog = true
      const res = await getPermission(id)
      this.form = res.data
      console.log(res)
      this.isEdit = true
    },
    async hDel(id) {
      await delPermission(id)
      this.$message.success('删除成功')
      this.getList()
    }
  }
}
</script>

<style>
.btn-add {
  margin: 10px;
}
</style>
